var is = require('./is');

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/*deepcopy*/
var hasOwnProperty = Object.prototype.hasOwnProperty;
var propIsEnumerable = Object.prototype.propertyIsEnumerable;

function toObject(val) {
	if (val === null || val === undefined) {
		throw new TypeError('Cannot convert undefined or null to object');
	}
	return Object(val);
}



function assignKey(to, from, key) {
	var val = from[key];
	if (val === undefined || val === null) {
		return;
	}
	if (hasOwnProperty.call(to, key)) {
		if (to[key] === undefined || to[key] === null) {
			throw new TypeError('Cannot convert undefined or null to object (' + key + ')');
		}
	}
	if (!hasOwnProperty.call(to, key) || !is.isObject(val)) {
		to[key] = val;
	} else {
		to[key] = assign(Object(to[key]), from[key]);
	}
}



function assign(to, from) {
	if (to === from) {
		return to;
	}
	from = Object(from);
	// 生命周期方法
	var lifekeys = ['onLoad', 'onReady' ,'onShow', 'onHide', 'onUnload']
	var lifes = {}
	lifekeys.forEach((key)=>{
		lifes[key] = to[key];
	});
	for (var key in from) {
		if(lifekeys.indexOf(key) != -1){
			(function(_key){
				to[_key] = function(options){
					if(lifes[_key]){
						lifes[_key].call(this,options);
					}
					from[_key].call(this,options);
				}

			})(key)
		}else{
			if (hasOwnProperty.call(from, key)) {
				assignKey(to, from, key);
			}
		}
		
	}
	if (Object.getOwnPropertySymbols) {
		var symbols = Object.getOwnPropertySymbols(from);
		for (var i = 0; i < symbols.length; i++) {
			if (propIsEnumerable.call(from, symbols[i])) {
				assignKey(to, from, symbols[i]);
			}
		}
	}
	return to;
}

function deepCopy(target){
    target = toObject(target);
    for (var s = 1; s < arguments.length; s++) {
		assign(target, arguments[s]);
	}
	return target;
}

function share(obj){
	return{
		onShareAppMessage: function () {
		    return {
				title: obj.title || '自定义分享标题',
				// desc: obj.desc || '自定义分享描述',
				path: obj.path || '/page/index'
		    }
		}
	}
	
}

module.exports = {
  formatTime: formatTime,
  assign:deepCopy,
  share:share
}